package com.llu.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.llu.entity.Borrow;
import com.llu.entity.UserBorrowView;
import com.llu.service.BorrowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (Borrow)表控制层
 *
 * @author makejava
 * @since 2024-08-23 10:51:08
 */
@RestController
@Slf4j
@RequestMapping("borrow")
public class BorrowController {
    /**
     * 服务对象
     */
    @Resource
    private BorrowService borrowService;

    /**
     * uid的用户向 bid 的书本借书
     * @param uid uid的用户
     * @param bid bid的书本
     * @return
     */
    @GetMapping("/take/{uid}/{bid}")
    JSONObject borrow(@PathVariable("uid") int uid,
                      @PathVariable("bid") int bid){
        borrowService.doBorrow(uid, bid);

        JSONObject object = new JSONObject();
        object.put("code", "200");
        object.put("success", false);
        object.put("message", "借阅成功！");
        return object;
    }


    @GetMapping("/slow")
    @SentinelResource(value = "testMethod", blockHandler = "block")
    public String slow() throws InterruptedException {
        Thread.sleep(500);
        return "这是一个慢比例调用！";
    }
//    服务降级
    public String block(BlockException e) {
        return "发生服务降级/熔断了";
    }


    /**
     * 分页查询
     *
     * @param borrow 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Borrow>> queryByPage(Borrow borrow, PageRequest pageRequest) {
        return ResponseEntity.ok(this.borrowService.queryByPage(borrow, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Borrow> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.borrowService.queryById(id));
    }

    // 通过uid 查找借阅记录
    @GetMapping("uid/{uid}")
    public ResponseEntity<UserBorrowView> getBorrowViewByUid(@PathVariable("uid") String uid , HttpServletRequest request) {
        log.info("book-service 的请求头: " + request.getHeader("Test"));
        return ResponseEntity.ok(this.borrowService.queryByUid(uid));
    }


    /**
     * 新增数据
     *
     * @param borrow 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Borrow> add(Borrow borrow) {
        return ResponseEntity.ok(this.borrowService.insert(borrow));
    }

    /**
     * 编辑数据
     *
     * @param borrow 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Borrow> edit(Borrow borrow) {
        return ResponseEntity.ok(this.borrowService.update(borrow));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.borrowService.deleteById(id));
    }

}

